package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbzMaterialList;
import com.jy.adscm.operation.mscyb.bean.JqbzMaterialListCheck;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzMaterialListCheckMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzMaterialListMapper;
import com.jy.adscm.operation.mscyb.service.JqbzMaterialListCheckService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.service.JqbzMaterialListService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-05-25
 */
@Service
public class JqbzMaterialListCheckServiceImpl extends ServiceImpl<JqbzMaterialListCheckMapper, JqbzMaterialListCheck> implements JqbzMaterialListCheckService {
    @Autowired
    JqbzMaterialListMapper  jqbzMaterialListMapper;
    @Autowired
    JqbzMaterialListService jqbzMaterialListService;


    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public List<JqbzMaterialListCheck> selectByPid(String pid) {
        Wrapper<JqbzMaterialListCheck> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        wrapper.orderBy("rank",false);
        List<JqbzMaterialListCheck> jqbzMaterialListChecks = super.selectList(wrapper);
        for (JqbzMaterialListCheck jqbzMaterialListCheck : jqbzMaterialListChecks) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzMaterialListCheck.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqbzMaterialListCheck.getCreator());
                jqbzMaterialListCheck.setCreatorName(jyUser.getName());
            }
        }
        return jqbzMaterialListChecks;
    }

    @Override
    public boolean updatejqbzMaterialListCheck(List<JqbzMaterialListCheck> jqbzMaterialListCheck, Token token)throws AdscmSystemException {

        for (JqbzMaterialListCheck materialListCheck : jqbzMaterialListCheck) {
            if ("结果".equals(materialListCheck.getName())){
                //查询到检查的物料
                JqbzMaterialList jqbzMaterialList = jqbzMaterialListMapper.selectById(materialListCheck.getPid());
                if ("1".equals(materialListCheck.getValue())){
                    jqbzMaterialList.setState("1");
                }
                if ("0".equals(materialListCheck.getValue())){
                    jqbzMaterialList.setState("0");
                }
                jqbzMaterialList.setCreator(token.getUser().getId().toString());
                jqbzMaterialList.setCreateTime(new Date());
                //修改这个物料
                jqbzMaterialListService.updateJqbzMaterialList(jqbzMaterialList);
            }
            boolean b = super.updateById(materialListCheck);
        }




        return true;
    }
}
